package com.nny.alarm.edit;


import java.text.DateFormatSymbols;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.nny.alarm.R;
import com.nny.alarm.Settings.Ring;
import com.uxwine.alarm.TimeCycle2;
import com.uxwine.alarm.alert.RingManager;
import com.uxwine.cmm.UxAdapter;
import com.uxwine.util.UxToast;
import com.uxwine.widget.ActionDialog;

public class RingtoneDialog extends ActionDialog implements 
		View.OnClickListener, OnItemClickListener, Callback
{

	RingEditor mCycleEditor;
	AlarmEditor mEditor;
	ListView mList;
	MAdapter mAdapter;
	long mlFlagRecord;
	RingtoneManager mRingtoneManager;
	int mnRingtoneCount;
	RingLoader mRingLoader;
	Handler mHandler;
	Ringtone mRingtoneSelect;
	
	static final int MSG_PLAY_RINGTONE_DELAY = 1;
	
	public RingtoneDialog(AlarmEditor editor, RingEditor ringEditor)
	{
		super(editor.mCtx, R.style.NomalDialog);
		mEditor = editor;
		mCycleEditor = ringEditor;
		mHandler = new Handler(this);
		mRingtoneSelect = null;
	}			
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.dlg_alarm_ringtone);
		setCanceledOnTouchOutside(true);
		
		final Context context = getContext();
		findViewById(R.id.alarm_ringtone_ok).setOnClickListener(this);
		findViewById(R.id.alarm_ringtone_cancel).setOnClickListener(this);
		ProgressBar pb = (ProgressBar)findViewById(R.id.dlg_alarm_ring_pb);
		
		mList = (ListView)findViewById(R.id.alarm_ringtone_list);
		
		mList.setEmptyView(pb);
		mAdapter = new MAdapter(mEditor.mAlarm.cycle);
		mList.setAdapter(mAdapter);
		mList.setOnItemClickListener(this);
		
		initRingtoneInfo(context, mEditor.mAlarm.alert);
	}	
	
	@Override
	public void onClick(View v) {
		int id = v.getId();
		if(id == R.id.alarm_ringtone_ok)
		{			
			mEditor.mAlarm.alert = mRingtoneManager.getRingtoneUri((int)mlFlagRecord);
			exit(ACTION_OK);
		}
		else if(id == R.id.alarm_ringtone_cancel)
		{
			exit(ACTION_CANCEL);
		}
	}

	
	
	@Override
	public void onDismiss(DialogInterface dialog) {
		super.onDismiss(dialog);

		if(null != mRingLoader)
			mRingLoader.cancel(true);
		
		stopRingtone();
	}
	
	public void stopRingtone()
	{
		mHandler.removeMessages(MSG_PLAY_RINGTONE_DELAY);
		if(null != mRingtoneSelect)
		{
			if(mRingtoneSelect.isPlaying())
			{
				mRingtoneSelect.stop();
				mRingtoneSelect = null;
			}
		}
	}

	private void initRingtoneInfo(Context ctx, Uri uri)
	{
		mRingtoneManager = new RingtoneManager(getContext());
		mRingtoneManager.setType(RingtoneManager.TYPE_ALARM);
		Cursor cur = mRingtoneManager.getCursor();
		if(cur == null)
			return;

		mRingLoader = new RingLoader();
		mnRingtoneCount = cur.getCount();
		mRingLoader.execute();
	}
	
	private class RingLoader extends AsyncTask<Void, Void, Void>
	{
		@Override
		protected Void doInBackground(Void... params) {

			Uri uri = mEditor.mAlarm.alert;
			int type = RingManager.getRingType(uri);
			mlFlagRecord = type == RingManager.TYPE_SYSTEM ? 0 : mRingtoneManager.getRingtonePosition(uri);
			
			ArrayList<Ringtone> arr = new ArrayList<Ringtone>();
			
			for(int i = 0; i < mnRingtoneCount; i++)
			{
				arr.add(mRingtoneManager.getRingtone(i));
			}
			mAdapter.setData(arr);
			return null;
		}

		@Override
		protected void onPostExecute(Void result) 
		{
			mAdapter.notifyDataSetChanged();
			mRingLoader = null;
		}
	}
	
	private class MAdapter extends UxAdapter<Ringtone>
	{
		LayoutInflater mInflater;
		
		public MAdapter(TimeCycle2 tc)
		{
			mInflater = LayoutInflater.from(getContext());
		}
		
		public void setData(ArrayList<Ringtone> src)
		{
			mData = src;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			Ringtone r = (Ringtone)getItem(position);
			convertView = mInflater.inflate(R.layout.listitem_alarm_cycle_hour, null);
			TextView tv = (TextView)convertView.findViewById(R.id.alarm_cycle_tv);
			CheckBox cb = (CheckBox)convertView.findViewById(R.id.alarm_cycle_cb);
			tv.setText(r.getTitle(getContext()));
			cb.setChecked(mlFlagRecord == position);
			
			convertView.setTag(r);
			return convertView;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,	long id)
	{
		mlFlagRecord = position;
		stopRingtone();
		
		mRingtoneSelect = (Ringtone)view.getTag();
		mHandler.sendEmptyMessageDelayed(MSG_PLAY_RINGTONE_DELAY, 1000);
		mAdapter.notifyDataSetChanged();
	}

	@Override
	public boolean handleMessage(Message msg)
	{
		if(msg.what == MSG_PLAY_RINGTONE_DELAY)
		{
			if(null != mRingtoneSelect)
			{
				mRingtoneSelect.play();
			}
		}
		return false;
	}
}
